package com.zhongke.cotrun.controller.manager;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.pojo.IntegralRecord;
import com.zhongke.cotrun.model.pojo.IntegralRecordVo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.IntegralRecordService;
import com.zhongke.cotrun.service.UserInfoService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import com.zhongke.cotrun.shiro.web.entity.WebUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/manager/IntegralRecord")
@Validated
public class IntegralRecordController {

    @Autowired
    IntegralRecordService integralRecordService;
    @Autowired
    UserInfoService userInfoService;


    /**
     * 按条件搜索积分规则
     *
     * @param integralRecord
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping("/selectIntegralRecord")
    public APIResponse selectIntegralRecord(IntegralRecord integralRecord, @RequestParam(required = false, defaultValue = "1") Integer pageNumber, @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return APIResponse.returnSuccess(integralRecordService.selectIntegralRecordByCondition(integralRecord, pageNumber, pageSize));

    }

    /**
     * 根据积分类型id查看积分规则
     *
     * @param integralRecordId
     * @return
     */
    @RequestMapping("/selectIntegralRecordByid")
    public APIResponse selectIntegralRecordByid(@NotNull(message = "积分规则id不能为空") Integer integralRecordId) {
        return APIResponse.returnSuccess(integralRecordService.selectIntegralRecordByid(integralRecordId));

    }


    /**
     * 根据积分类型id查看积分规则
     *
     * @param IntegralNameId
     * @return
     */
    @RequestMapping("/selectIntegralRecordByIntegralNameId")
    public APIResponse selectIntegralRecordByIntegralNameId(@NotNull(message = "积分类型id不能为空") Integer IntegralNameId) {
        return APIResponse.returnSuccess(integralRecordService.selectIntegralRecordByIntegralType(IntegralNameId));

    }


    /**
     * 新建修改积分规则
     *
     * @param integralRecord
     * @return
     */

    @PostMapping("/insertUpdateIntegral")
    public APIResponse insertUpdateIntegral(IntegralRecord integralRecord) {

        if (integralRecordService.insertUpdateIntegralRecord(integralRecord)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.ADD_ERROR);

    }

    /**
     * 查看自己的权益最大的
     * rankOne 1000赚积分 2000花积分
     *
     * @param integralRecordVo
     * @return
     */
    @PostMapping("/selectMyRights")
    public APIResponse selectMyRights(IntegralRecordVo integralRecordVo) {

        WebUserEntity w = ShiroUtils.getWebUser();
        if (w == null) {
            return APIResponse.returnFail(CodeMsg.USER_LOGIN_ERROR);
        }

//        UserInfo userInfo1 = userInfoService.selectUserInfoByid(userInfo.getUserId());

        List l = integralRecordService.selectOfMyBig(integralRecordVo, "VIP0", w );
        return APIResponse.returnSuccess(l);

    }


}
